You can use several concepts to pass values from controller to views or from one view to another view. If you want to pass temporary values (which is only for one http request) then use TempData collection otherwise you can use ViewBag and ViewData collection.
TempData
TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this. Therefore, the only scenario where using TempData will reliably work is when you are redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view.
ViewData
Viewdata is the collection of state that is passed to a view. A view should get all the information it needs from the viewdata. It contains key/value pairs as well as some special properties, such as Model. Viewdata is accessible on the controller as well. The controller is responsible for filling the viewdata dictionary with objects. When a view is executing, it will pull various object from viewdata while it is rendering. If an expected object is not present, then you will see the same type of exception present in any code using a dictionary collection in .Net.
ViewBag
ViewBag is a property of a Controller class that we generally used to share data between Controller and View and. ViewBag objects is wrapper around ViewData that is used to create dynamic properties for ViewBag.
The basic difference between ViewData and ViewBag is that in ViewData instead creating dynamic properties we use properties of Model to transport the Model data in View and in ViewBag we can create dynamic properties without using Model data.
Anonymous User
16-Apr-2019Nice Blog.
Chris Anderson
16-Dec-2011